<?php
if(!defined('MY_ROOT'))
{
throw new Exception('Access Denied');
}
require_once(MY_ROOT.'/source/class/db/mysql.php');
class DB{
public static $db;
public static function init($config=array())
{
self::$db=new mysql($config);
}
public static function select($table,$where,$order='`id`',$first=false,$col="*")
{
$sql="SELECT ".$col." FROM `".self::$db->tpre($table)."` WHERE ".$where." ORDER BY ".$order;
if($first)
{
$result=self::$db->fetch_first($sql);

}else{
$result=self::$db->fetch_all($sql);
}
return $result;
}
public static function update($table,$sets,$where)
{
$sql="UPDATE `".self::$db->tpre($table)."` SET ";
foreach($sets as $key =>$value)
{
if(!is_numeric($value))
{
$value=self::$db->get_str($value);
}
$sql.="`".$key."` =".$value.",";
}
$sql=substr($sql,0,-1);
$sql.="WHERE ".$where;
return self::$db->query($sql);
}
public static function insert($table,$sets=array())
{
$sql="INSERT INTO `".self::$db->tpre($table)."`(";
if(is_array($sets[0])){
foreach(array_keys($sets[0]) as $key)
{
$sql.='`'.$key.'`,';
}
$sql=substr($sql,0,-1);
$sql.=') VALUES ';
foreach($sets as $set)
{
$sql.="(";
foreach($set as $value)
{
if(!is_numeric($value))
{$sql.=self::$db->get_str($value);}else{
	$sql.=$value;
	}
$sql.=$value.',';
}
$sql=substr($sql,0,-1);
$sql.="),";
}
$sql=substr($sql,0,-1);
}else{
foreach(array_keys($sets) as $key)
{
$sql.='`'.$key.'`,';
}
$sql=substr($sql,0,-1);
$sql.=") VALUES (";
foreach($sets as $value)
{
if(!is_numeric($value))
{$sql.=self::$db->get_str($value);}
else{
	$sql.=$value;
	}
$sql.=",";
}
$sql=substr($sql,0,-1);
$sql.=')';
}
return self::$db->query($sql);
}
public static function delete($table,$condition)
{
if($condition=="")
{
return -1;
}
$sql="DELETE FROM `".self::$db->tpre($table)."` WHERE ".$condition;
return self::$db->query($sql);
}
public static function opt($table)
{
$table=trim($table);
$sql="OPTIMIZE TABLE `".$table."`";
return self::$db->query($sql);
}
public static function ijoin($stable,$atable,$acol,$acolw,$scol,$cond,$order='id',$first=false)
{
	$sql="SELECT ".self::$db->tpre($stable).".*,`".self::$db->tpre($atable)."`.`".$acol."` FROM `".self::$db->tpre($stable)."` INNER JOIN `".self::$db->tpre($atable)."` ON `".$atable."`.`".$acolw. "` = `".self::$db->tpre($stable)."`.`".$scol."` WHERE ".$cond." ORDER BY `".self::$db->tpre($stable).'`.`'.$order.'`';
if($first)
{
$result=self::$db->fetch_first($sql);

}else{
$result=self::$db->fetch_all($sql);
}
return $result;
}
public static function query($sql)
{
return self::$db->query($sql);
}

public static function get_str($str)
{
return self::$db->get_str($str);
}
public static function tpre($table)
{
return self::$db->tpre($table);
}
}
?>
